/*
 * @Description: 
 * @Date: 2021-08-26 15:12:08
 * @Author: 张自文
 * @LastEditTime: 2021-08-27 09:29:22
 * @LastEditors: 张自文
 * @FilePath: \flutter_demo_list\lib\pages\image_cutting\iamge_cut.dart
 */
import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';

void main() => runApp(new ImageCut());

class ImageCut extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '图片上传裁剪',
      theme: ThemeData.light()
          .copyWith(primaryColor: const Color.fromRGBO(70, 176, 254, 1)),
      home: ImageCutPage(
        title: '图片上传裁剪',
      ),
    );
  }
}

class ImageCutPage extends StatefulWidget {
  final String title;

  ImageCutPage({required this.title});

  @override
  _ImageCutPageState createState() => _ImageCutPageState();
}

enum AppState {
  free,
  picked,
  cropped,
}

class _ImageCutPageState extends State<ImageCutPage> {
  late AppState state;
  File? imageFile;

  @override
  void initState() {
    super.initState();
    state = AppState.free;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: imageFile != null ? Image.file(imageFile!) : Container(),
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.black,
        onPressed: () {
          if (state == AppState.free)
            _pickImage();
          else if (state == AppState.picked)
            _cropImage();
          else if (state == AppState.cropped) _clearImage();
        },
        child: _buildButtonIcon(),
      ),
    );
  }

  Widget _buildButtonIcon() {
    if (state == AppState.free)
      return Icon(Icons.add);
    else if (state == AppState.picked)
      return Icon(Icons.crop);
    else if (state == AppState.cropped)
      return Icon(Icons.clear);
    else
      return Container();
  }

  //图片上传
  Future<Null> _pickImage() async {
    final pickedImage =
        await ImagePicker().pickImage(source: ImageSource.gallery);
    imageFile = pickedImage != null ? File(pickedImage.path) : null;
    if (imageFile != null) {
      setState(() {
        state = AppState.picked;
      });
    }
  }

  //图片裁剪
  Future<Null> _cropImage() async {
    File? croppedFile = await ImageCropper.cropImage(
        sourcePath: imageFile!.path,
        maxWidth: 200,
        maxHeight: 200,
        aspectRatio: const CropAspectRatio(ratioX: 10, ratioY: 10),
        cropStyle: CropStyle.circle,
        androidUiSettings: const AndroidUiSettings(
            toolbarTitle: '裁剪图片',
            toolbarColor: Colors.black,
            toolbarWidgetColor: Colors.white,
            lockAspectRatio: false),
        iosUiSettings: IOSUiSettings(
          title: '裁剪图片',
        ));
    if (croppedFile != null) {
      imageFile = croppedFile;
      setState(() {
        state = AppState.cropped;
      });
    }
  }

  void _clearImage() {
    imageFile = null;
    setState(() {
      state = AppState.free;
    });
  }
}
